package enterprise.manage.web.validation;

import java.io.Serializable;

import org.apache.commons.validator.*;
import org.apache.commons.validator.util.ValidatorUtils;
import org.springframework.validation.Errors;
import org.springmodules.validation.commons.FieldChecks;
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;

/**
 * Created by IntelliJ IDEA.
 * User: 乔莹
 * Date: 2009-6-30
 * Time: 17:31:20
 * Desc: validwhen验证
 */
public class ValidateTwoFields implements Serializable {
    private static Logger logger = LoggerFactory.getLogger(ValidateTwoFields.class);

    public static boolean validateTwoFields(Object bean, ValidatorAction va,
                                            Field field, Errors errors) {
        String value1 = ValidatorUtils.getValueAsString(bean, field.getProperty());
        String value2 = ValidatorUtils.getValueAsString(bean, field.getVarValue("secondProperty"));
        logger.debug("value1=" + value1 + "***************value2=" + value2);
        if (!value1.equals(value2)) {
            FieldChecks.rejectValue(errors, field, va);
            return false;
        } else {
            return true;
        }
    }
}
